package com.yu.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendMsg/{message}")
    private void sendMsg(@PathVariable("message") String message){
        log.info("当前时间:{},发送一条消息给两个延时队列",new Date(),message);
        rabbitTemplate.convertAndSend("X","XA","消息来自10的队列:"+message);
        rabbitTemplate.convertAndSend("X","XB","消息来自40的队列:"+message);
    }


    @RequestMapping("sendExpirationMsg/{message}/{ttlTime}")
    private void sendExpirationMsg(@PathVariable("message") String message,
                                   @PathVariable("ttlTime") String ttlTime){
       rabbitTemplate.convertAndSend("X","XC",message,correlationData ->{
           correlationData.getMessageProperties().setExpiration(ttlTime);
           return correlationData;
       });
    }

}
